package com.example.skipad

import android.content.Context
import android.util.Log
import android.view.accessibility.AccessibilityNodeInfo
import kotlinx.coroutines.*

//
// Created by Kong on 2021/9/13.
// Copyright (c) 2021 kong. All rights reserved.
//
object SkipUtil {

    private const val detectionTime = 1500L
    private const val intervalTime = 200L

    private var coroutineScope: CoroutineScope? = null

    private val whiteList = arrayListOf(
        "com.oppo.launcher",
        "com.android.launcher",
        "com.android.systemui",
        "com.miui.home",
        "com.huawei.android.launcher",
        "com.sec.android.app.launcher",
        "com.meizu.flyme.launcher",
        "com.meizu.net.search",
        "com.android.settings",
        "com.tencent.mm",
        "com.tencent.tim",
        "com.tencent.qq",
        "com.tencent.tmgp.sgame",
    )


    fun action(
        applicationContext: Context,
        nodeInfo: AccessibilityNodeInfo
    ) {

        coroutineScope?.cancel()

        if (nodeInfo.packageName == null) {
            return
        }

        val packageName = nodeInfo.packageName.toString()

        //自身返回
        if (applicationContext.packageName == packageName) {
            Log.d("WindowChange", "onAccessibilityEvent: 自身跳过成功")
            return
        }

        //在白名单内的拦截
        if (whiteList.contains(packageName)) {
            return
        }

        when (packageName) {
            //网易云音乐
            "com.netease.cloudmusic" -> skip(nodeInfo.findAccessibilityNodeInfosByViewId("com.netease.cloudmusic:id/c3l"))
            //最右
            "cn.xiaochuankeji.zuiyouLite" -> skip(
                nodeInfo.findAccessibilityNodeInfosByViewId("cn.xiaochuankeji.zuiyouLite:id/btn_skip")
            )
            //天府市民云
            "com.tfsmy.apps.android.tfsmy" -> skip(nodeInfo.findAccessibilityNodeInfosByViewId("com.tfsmy.apps.android.tfsmy:id/iv_skip"))
            //12306
            "com.MobileTicket" -> skip(nodeInfo.findAccessibilityNodeInfosByViewId("com.MobileTicket.launcher:id/tv_skip"))
            //我
            else -> {
                when (packageName) {
                    "com.ss.android.ugc.aweme" -> {
                        //抖音极速版本  青少年弹窗关闭
                        skip(nodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/ahr"))
                    }
                    else -> {
                    }
                }


                defaultSkip(nodeInfo)
            }
        }
    }


    private fun skip(nodeInfoList: List<AccessibilityNodeInfo>) {
        runBlocking<Unit> {
            try {
                coroutineScope = this
                withTimeoutOrNull(detectionTime) {
                    while (true) {
                        Log.d("WindowChange", "onAccessibilityEvent: 轮循")
                        if (nodeInfoList.isNotEmpty()) {
                            val accessibilityNodeInfo = nodeInfoList[0]
                            accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                            break
                        }
                        delay(intervalTime)
                    }
                }
            } catch (e: Exception) {
                Log.e("Exception", "skip: ", e)
            }
        }


    }


    private fun defaultSkip(nodeInfo: AccessibilityNodeInfo) {
        runBlocking<Unit> {

            coroutineScope = this
            try {
                withTimeoutOrNull(detectionTime) { // Timeout after 2000ms
                    while (true) {
                        val nodeList = nodeInfo.findAccessibilityNodeInfosByText("跳过")
                        if (nodeList.isNotEmpty()) {
                            nodeList[0].performAction(AccessibilityNodeInfo.ACTION_CLICK)
                            nodeList[0].parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                            break
                        }
                        Log.d("WindowChange", "onAccessibilityEvent: 轮循")
                        delay(intervalTime)
                    }
                }
            } catch (e: Exception) {
                Log.e("Exception", "onAccessibilityEvent: ", e)
            }
        }
    }


}